package com.xinruke.hostels.manage.user.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinruke.hostels.common.controller.BaseController;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.manage.user.interfaces.dto.*;
import com.xinruke.hostels.manage.user.service.RightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName RightsController
 * @CreateDate 2019/6/12
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 权限管理
 */
@RestController
@RequestMapping("rights")
@Api(tags = "权限管理")
public class RightsController extends BaseController {
    @Resource
    OperateLogService operateLogService;
    @Resource
    RightService rightService;


    @PostMapping(value = "/by_role_id")
    @ApiOperation(value = "通过角色查询权限信息")
    public ResultVO<List<RoleRightsQueryResultDTO>> getRightsListByRoleId(@Valid @RequestBody RoleRightsQueryDTO queryDTO) {
        ResultVO<List<RoleRightsQueryResultDTO>> resultVO = ResultVO.fail();
        List<RoleRightsQueryResultDTO> resultVos = rightService.getRightsListByRoleId(queryDTO);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(resultVos);
        return resultVO;
    }

    @PostMapping(value = "/by_user_id")
    @ApiOperation(value = "通过用户查询权限信息")
    public ResultVO<List<MenusAndButtonsDto>> getRightsListByUserId(@Valid @RequestBody UserRightsQueryDTO queryDTO) {
        return rightService.getRightsListByUserId(queryDTO);

    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "(重要接口谨慎添加)添加菜单和按钮")
    public ResultVO add(@Valid @RequestBody List<ButtonListQueryDTO> buttonListQueryDTO) {
        return rightService.addButtonList(buttonListQueryDTO);

    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "组装权限树")
    public ResultVO<List<MenusAndButtonsDto>> rightsTree(@Valid @RequestBody RightsQueryDTO rightsQueryDTO) {
        ResultVO<List<MenusAndButtonsDto>> resultVO = ResultVO.fail();
        List<MenusAndButtonsDto> resultVos = rightService.rightsTree(rightsQueryDTO);
        resultVO.setCode(ResultVO.SUCCESS);
        resultVO.setMessage("操作成功");
        resultVO.setData(resultVos);
        return resultVO;
    }

    @PostMapping(value = "/assign")
    @ApiOperation(value = "分配权限")
    public ResultVO assign(@Valid @RequestBody RightAssignDto rightAssignDto) {
        ResultVO resultVO = ResultVO.fail();
        int r = rightService.assign(rightAssignDto);
        if (r > 0) {
            operateLogService.writeOperateLog("权限", "分配权限", "操作成功");
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("操作成功");
        }
        return resultVO;
    }


}
